package com.peng.leetcode.array;

import java.util.TreeSet;

/**
 * ContainsNearbyAlmostDuplicate
 * <p>
 * 220. 存在重复元素 III
 *
 * @author: lupeng6
 * @create: 2021/4/19 15:19
 */
public class ContainsNearbyAlmostDuplicate {


    public static void main(String[] args) {

    }


    public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
        int n = nums.length;
        TreeSet<Long> set = new TreeSet<>();

        for (int i = 0; i < n; i++) {
            int num = nums[i];
            Long ceiling = set.ceiling((long) num - (long) t);
            if (ceiling != null && ceiling <= num + t) {
                return true;
            }
            set.add((long) num);
            if (i >= k) {
                set.remove((long) nums[i - k]);
            }
        }
        return false;
    }


}
